<?php
/**
 * 框架总入口控制器
 * @authors yutent (yutent@doui.cc)
 * @date    2014-10-04 23:18:52
 * @version 1.0
 */

class D {

    private static $init = NULL;

    private static $log = NULL;

    private function __construct(){
        global $_G;

        $_G['route'] = new Do_Router;
        $_G['route']->route('/:id', function($router){
            new $router;
        });
    }

    /**
     * [run 运行框架]
     */
    public static function run(){
        if (self::$init === NULL) {
            self::$init = new D;
        }
        return self::$init;
    }


    /**
     * [get 处理过的参数获取]
     * @param  string $para [要获取的字段]
     * @return
     */
    public static function get($para = ''){
        if(empty($para)){
            $data = [];
            foreach ($_GET as $key => $value) {
                $data[$key] = trim(addslashes($value));
            }
            return $data;
        }
        if(isset($_GET[$para])){
            return trim(addslashes($_GET[$para]));
        }else{
            return NULL;
        }
    }

    /**
     * [post 处理过的参数获取]
     * @param  string $para [要获取的字段]
     * @return
     */
    public static function post($para = ''){
        if(empty($para)){
            $data = [];
            foreach ($_POST as $key => $value) {
                $data[$key] = trim(addslashes($value));
            }
            return $data;
        }
        if(isset($_POST[$para])){
            return trim(addslashes($_POST[$para]));
        }else{
            return NULL;
        }
    }

    /**
     * [load 加载应用 Model 或者 自定义文件]
     * @param  [type] $class [Model名称或文件名，文件名需带后缀]
     * @param  [type] $path  [文件路径, 加载Model不用加这个参数]
     */
    public static function load($class, $path = NULL){

        $file = $path === NULL ? APPS . $class . '/model/index.m.php' : $path . $class;

        if(!require_cache($file)){
            self::log($file . ' 加载失败');
        }
    }

    /**
     * [ip 获取ip地址]
     * @return
     */
    public static function ip(){
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
        if (!ip2long($ip)) $ip = '0.0.0.0';
        
        return $ip;
    }

    /**
     * [log 全局日志记录]
     * @param  string $str [要记录的内容]
     */
    public static function log($str = ''){
        global $_G;

        if(self::$log === NULL){
            self::$log = new Do_Log('run_time.log');
        }
        self::$log->write($str);
    }

    /**
     * [end 以json格式输出内容]
     * @param  integer $i        [错误码代号]
     * @param  string  $msg      [提示信息]
     * @param  array   $data     [附加数据，可以是字符串，对象，默认是数组]
     * @param  boolean $callback [是否jsonp]
     * 
     */
    public static function end($i = 0, $msg = '', $data = [], $callback = FALSE){
        if(!is_numeric($i)){
            $msg = $i;
            $i = 0;
        }elseif(is_array($msg)){
            $data = $msg;
            $msg = 'success';
            $i = 1;
        }
        $res = ['result'=>$i,'msg'=>$msg, 'data' => $data];

        header('Content-type:application/json');
        if($callback){
            $callback = addslashes($callback);
            echo $callback, '(', json_encode($res), ')';
        }else{
            echo json_encode($res);
        }
    }


}

